//
//  YeeLoginVC.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/7.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeLoginVC.h"
#import "YeeLoginViewModel.h"
#import "XRSelfLoginShareFootView.h"
#import "YeeSignVC.h"      //注册
#import "YeeForgetVC.h"    //重置密码
@interface YeeLoginVC ()
{
    UITextField   *_phoneTextField;
    UITextField   *_pwdTextField;
    UIButton      *_loginButton;
    UIButton      *_coloseButton;
    UIButton      *_signButton;
    UIButton      *_forgetButton;
    XRSelfLoginShareFootView  *_footView;
    YeeLoginViewModel  *_loginViewModel;

}
@end

@implementation YeeLoginVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
-(void)addOwnViews
{
    [super addOwnViews];
    [self.view setBackgroundColor:kWhiteColor];
    _loginButton=[UIButton buttonWithType:UIButtonTypeCustom];
    _loginButton.layer.masksToBounds=YES;
    _loginButton.layer.cornerRadius=17.5;
    _loginButton.enabled=NO;
    [_loginButton setFrame:CGRectMake(15, 160, kMainScreenWidth-30, 35)];
    _loginButton.centerY=self.view.centerY;
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    _loginButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [_loginButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [_loginButton setBackgroundImage:[UIImage imageWithColor:kGrayColor] forState:UIControlStateDisabled];
    [_loginButton setBackgroundImage:[UIImage imageWithColor:kNavBarThemeColor] forState:UIControlStateNormal];
    [self.view addSubview:_loginButton];
    _pwdTextField=[[UITextField alloc] initWithFrame:CGRectMake(15, CGRectGetMinY(_loginButton.frame)-60, kMainScreenWidth-30, 35)];
    _pwdTextField.layer.borderColor=kNavBarThemeColor.CGColor;
    _pwdTextField.layer.borderWidth=0.3;
    _pwdTextField.layer.cornerRadius=17.50f;
    _pwdTextField.layer.masksToBounds=YES;
    _pwdTextField.font=systemfont(14);
    _pwdTextField.leftSpace=10.0;
    _pwdTextField.placeholder=@"请输入密码";
    [self.view addSubview:_pwdTextField];
    _phoneTextField=[[UITextField alloc] initWithFrame:CGRectMake(15, CGRectGetMinY(_pwdTextField.frame)-60, kMainScreenWidth-30, 35)];
    _phoneTextField.layer.borderColor=kNavBarThemeColor.CGColor;
    _phoneTextField.layer.borderWidth=0.3;
    _phoneTextField.layer.cornerRadius=17.50f;
    _phoneTextField.layer.masksToBounds=YES;
    _phoneTextField.leftSpace=10.0;
    _phoneTextField.font=systemfont(13);
    _phoneTextField.placeholder=@"请输入手机号码";
    [self.view addSubview:_phoneTextField];
    _coloseButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_coloseButton setFrame:CGRectMake(kMainScreenWidth-50, 44, 45, 45)];
    [_coloseButton setImage:[UIImage imageNamed:@"icon_shezhi_weidianji"] forState:UIControlStateNormal];
    [self.view addSubview:_coloseButton];
    @weakify(self);
    [[_coloseButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x)
    {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }];
    
    _forgetButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_forgetButton setFrame:CGRectMake(CGRectGetMinX(_loginButton.frame)+10,CGRectGetMaxY(_loginButton.frame), 60, 45)];
    [_forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    _forgetButton.titleLabel.font=systemfont(13);
    [_forgetButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.view addSubview:_forgetButton];
    [[_forgetButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x)
     {
         @strongify(self);
         YeeForgetVC *forgetVc=[YeeForgetVC new];
         [self.navigationController pushViewController:forgetVc animated:YES];
     }];
    _signButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_signButton setFrame:CGRectMake(CGRectGetMaxX(_loginButton.frame)-60,CGRectGetMaxY(_loginButton.frame), 60, 45)];
    [_signButton setTitle:@"注册" forState:UIControlStateNormal];
    _signButton.titleLabel.font=systemfont(13);
    [_signButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.view addSubview:_signButton];
    [[_signButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x)
     {
         @strongify(self);
         YeeSignVC *signVc=[YeeSignVC new];
         [self.navigationController pushViewController:signVc animated:YES];
     }];
    _footView=[[XRSelfLoginShareFootView alloc] init];
    [self.view addSubview:_footView];
    [_footView mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(-50);
        make.height.mas_equalTo(110);
    }];
}
-(void)configOwnViews
{
    [super configOwnViews];
}
-(void)addNavbarView
{
    [super addNavbarView];
}
//添加底部第三方登录
-(void)addbottomView
{
   
    
    
}
#pragma mark 绑定ViewModel
-(void)bindViewModel
{
    [super bindViewModel];
    _loginViewModel=[[YeeLoginViewModel alloc] init];
    RAC(_loginViewModel,phoneString)=_phoneTextField.rac_textSignal;
    RAC(_loginViewModel,pwdString)=_pwdTextField.rac_textSignal;
    RAC(_loginButton,enabled)=_loginViewModel.loginneableSignal;
    //@weakify(self);
    [[_loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x)
     {
       RACSignal *requesSiganl = [_loginViewModel.command execute:nil];
        [requesSiganl  subscribeNext:^(id  _Nullable x)//用于接收数据
         {
            // @strongify(self);
             //[self.view showNBLoadingMessage:@"努力加载中..."];
        }];
         
     }];
   [[_pwdTextField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value)
    {
        RACSignal *Signal=[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [subscriber sendNext:value];
            [subscriber sendCompleted];
            return nil;
        }];
        
        return Signal;
    }] subscribeNext:^(id  _Nullable x)
    {
        NSLog(@"yuwei%@",x);
        
    }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
